package com.Serverce;

import com.DAO.Administorinfo;
import com.Module.AdministorM;
import com.Module.Boxnum;
import com.Util.Window;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.List;

public class Modify {
    Modify(){
        JFrame jf=new Window().window("修改密码",400,300,400,300);
        List<Boxnum> list_id=getlist("编        号",15,20);
        Box box_id=list_id.get(0).getBox();
        JTextField textField_id=list_id.get(0).getjTextField();

        List<Boxnum> list_pas=getlist("密        码",15,20);
        Box box_pas=list_pas.get(0).getBox();
        JTextField textField_pas=list_pas.get(0).getjTextField();


        List<Boxnum> list_pas_again=getlist("确认密码",15,20);
        Box box_pas_again=list_pas_again.get(0).getBox();
        JTextField textField_pas_again=list_pas_again.get(0).getjTextField();

        Box btnBox=Box.createHorizontalBox();
        JButton jb1=new JButton("修        改");
        jb1.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id_=textField_id.getText();
                String password=textField_pas.getText();
                String password_again=textField_pas_again.getText();
                if (id_.equals("") || password_again.equals("") || password.equals("")) {
                    JOptionPane.showMessageDialog(null,"不能输入空值","错误",JOptionPane.WARNING_MESSAGE);
                }else{
                    if (!password_again.equals(password)) {
                        JOptionPane.showMessageDialog(null,"两次密码不一致","错误",JOptionPane.WARNING_MESSAGE);
                    }else {
                        Administorinfo administorinfo=new Administorinfo();
                        AdministorM administorM=new AdministorM(id_,password_again);
                        Object obj=(Object) administorM;
                        int ans=administorinfo.modify(obj);
                        if (ans==0) {
                            JOptionPane.showMessageDialog(null,"修改失败","错误",JOptionPane.WARNING_MESSAGE);
                        }else {
                            JOptionPane.showMessageDialog(null,"修改成功","成功",JOptionPane.OK_OPTION);
                        }
                    }
                }
            }
        });
        JButton jb2=new JButton("退        出");
        jb2.addActionListener(e -> jf.dispose());
        btnBox.add(jb1);
        btnBox.add(Box.createHorizontalStrut(50));
        btnBox.add(jb2);

        Box vbox=Box.createVerticalBox();
        vbox.add(Box.createVerticalStrut(30));
        vbox.add(box_id);
        vbox.add(Box.createVerticalStrut(20));
        vbox.add(box_pas);
        vbox.add(Box.createVerticalStrut(20));
        vbox.add(box_pas_again);
        vbox.add(Box.createVerticalStrut(20));
        vbox.add(btnBox);

        JPanel jPanel=new JPanel();
        jPanel.add(vbox);
        jf.add(jPanel);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
    public static List<Boxnum> getlist(String title, int column, int width){
        Window window=new Window();
        List<Boxnum> list=window.HorizontalBox(title,column,width);
        return  list;
    }
}
